package com.home.webapp.base.service.impl;

import com.home.base.core.framework.msg.ServerResponse;
import com.home.common.framework.core.entity.EntityUtils;
import com.home.common.framework.core.service.impl.BaseServiceImpl;
import com.home.common.handler.exception.BizException;
import com.home.common.model.CurrentUser;
import com.home.common.model.TreeNode;
import com.home.webapp.base.entity.BasePermsAuth;
import com.home.webapp.base.mapper.BasePermsAuthMapper;
import com.home.webapp.base.service.BasePermsAuthService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Description: 权限下发 serviceImpl
 * Created by 王大宸 on 2020-10-05 23:28:38
 * Created with IntelliJ IDEA.
 */
@Service
public class BasePermsAuthServiceImpl extends BaseServiceImpl<BasePermsAuthMapper, BasePermsAuth, String> implements BasePermsAuthService {
    private static final Logger logger = LoggerFactory.getLogger(BasePermsAuthServiceImpl.class);

    /***
     * 功能说明：添加下发权限
     *
     * @author 王大宸
     * @date 2020/10/8 15:47
     * @param entity
     * @return com.home.common.framework.msg.ServerResponse<com.home.webapp.base.entity.BasePermsAuth>
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BasePermsAuth saveInfo(BasePermsAuth entity) throws BizException {
        BasePermsAuth permsAuth = new BasePermsAuth();
        permsAuth.setUserId(entity.getUserId());
        baseMapper.delete(permsAuth);

        String menuIds = entity.getMenuId();
        List<String> menuList = new ArrayList<>(Arrays.asList(menuIds.split(",")));
        if (0 == menuList.size()) {
            return entity;
        }

        BasePermsAuth one = null;
        List<BasePermsAuth> list = new ArrayList<>();
        String userId = entity.getUserId();
        for (String menuId : menuList) {
            if ("1".equals(menuId)) {
                continue;
            }
            one = new BasePermsAuth();
            one.setId(EntityUtils.getId());
            one.setMenuId(menuId);
            one.setUserId(userId);
            list.add(one);
        }
        baseMapper.batchSave(list);
        return entity;
    }

    /***
     * 功能说明：获取可下发的菜单树
     *
     * @author 王大宸
     * @date 2020/10/8 15:37
     * @param userId 用户id
     * @param user   当前登录用户
     * @return com.home.common.framework.msg.ServerResponse<java.util.List < java.util.Map < java.lang.String, java.lang.Object>>>
     */
    @Override
    public ServerResponse<List<TreeNode>> zTree(String userId, CurrentUser user) {
        List<TreeNode> list = new ArrayList<>();
        List<TreeNode> appList = new ArrayList<>();
        if ("admin".equals(user.getUsername())) {
            list = baseMapper.getMenuTree();
            appList = baseMapper.getAppTree();
        } else {
            list = baseMapper.getPermsAuth(user.getId());
            appList = baseMapper.getPermsAuthApp(user.getId());
        }
        list.addAll(appList);

        List<String> menuIds = baseMapper.getMenuByUserId(userId);
        for (TreeNode treeNode : list) {
            if (menuIds.contains(treeNode.getId())) {
                treeNode.setChecked(true);
            }
        }
        list.add(new TreeNode("1", "请选择应用", "0", true, "1"));
        return ServerResponse.okData(list);
    }
}
